﻿
@{
    ViewBag.Title = "Index";
}

<h2 style="color:red;">Phân Vai trò</h2>
@foreach(KhoaLuan.Models.ApplicationUser u in ViewBag.Users )
{
     var roles = u.Roles.Select(ur => ur.RoleId).Distinct().ToList();
    <div class="">
        <fieldset class="user-role">
            <legend>@u.UserName</legend>
            <ul>
                @foreach (Microsoft.AspNet.Identity.EntityFramework.IdentityRole r in ViewBag.Roles)
                {
                    <li>
                        <label>
                            @if (roles.Contains(r.Id))
                            {
                                <input checked type="checkbox" data-role="@r.Id" data-user="@u.Id" />@r.Name
                            }
                            else
                            {
                                <input type="checkbox" data-role="@r.Id" data-user="@u.Id" />@r.Name
                            }
                        </label>
                    </li>
                }
            </ul>
        </fieldset>
    </div>
}

@section scripts{
    
    <script>
        $(function () {
            $(".user-role :checkbox").click(function () {
                var rid = $(this).attr("data-role");
                var uid = $(this).attr("data-user");

                $.ajax({
                    url: "/Admin/UserRole/Update",
                    data: { RoleId: rid, UserId: uid },
                    success: function (response) {
                        //alert(response);
                    }
                });
            });
        });
    </script>


    
}